package cn.wenhaha.app.job.kit;

import cn.hutool.core.util.StrUtil;
import cn.wenhaha.app.mapping.bean.MappingType;
import cn.wenhaha.app.mapping.bean.dto.LookUp;
import cn.wenhaha.app.mapping.bean.dto.MappingObj;
import cn.wenhaha.sync.core.Column;
import cn.wenhaha.sync.core.ColumnType;
import cn.wenhaha.sync.core.QueryLookUp;

import java.util.ArrayList;
import java.util.List;

/**
 * 同步映射常用工具
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2022-12-07 21:37
 */
public class MappingKit {

    /**
     * <p>
     *    获取主键
     * </p>
     * @Author: Wyndem
     * @DateTime: 2022-12-07 21:39
     */
    public static MappingObj getKeyFileName(List<MappingObj> source) {
        return source.stream().filter(MappingObj::getPrimaryKey)
                .findFirst().orElseThrow(() -> new RuntimeException("无主键"));

    }



    /**
     * <p>
     *    MappingObj 映射转换插件可识别的字段信息
     * </p>
     * @Author: Wyndem
     * @DateTime: 2022-12-04 21:14
     */
    public static  List<Column> querySyncFields(List<MappingObj> source) {
        List<Column> colList = new ArrayList<>(source.size());
        List<String> columnList = new ArrayList<>(source.size());

        for (MappingObj col : source) {
            MappingType mappingType = MappingType.findByQueryType(col.getType());
            if (mappingType == null) {
                continue;
            }
            if (mappingType == MappingType.column && StrUtil.isNotEmpty(col.getColumn())
                    && !columnList.contains(col.getColumn())) {
                colList.add(Column.builder()
                        .column(col.getColumn())
                        .type(ColumnType.column)
                        .build());
                columnList.add(col.getColumn());
            } else if (mappingType == MappingType.constant) {
                colList.add(Column.builder()
                        .column(col.getNameApi()+"_"+MappingType.constant.getId())
                        .type(ColumnType.constant)
                        .value(col.getConstant())
                        .build());
            } else if (mappingType == MappingType.custom) {
                colList.add(Column.builder()
                        .column(col.getNameApi()+"_"+MappingType.custom.getId())
                        .type(ColumnType.custom)
                        .value(col.getCustom())
                        .build());
            } else if (mappingType == MappingType.sourceLookup) {
                LookUp sourceLookup = col.getSourceLookup();
                QueryLookUp lookUp = QueryLookUp.builder()
                        .objName(sourceLookup.getObjName())
                        .column(sourceLookup.getColumn())
                        .linkColumn(sourceLookup.getLinkColumn())
                        .currentColumn(sourceLookup.getCurrentColumn()).build();
                colList.add(Column.builder()
                        .column(col.getNameApi()+"_"+MappingType.sourceLookup.getId())
                        .type(ColumnType.sourceLookup)
                        .value(lookUp)
                        .build());
            }

        }

        return colList;
    }



    /**
     * <p>
     *    检查是否有对方配置
     * </p>
     * @Author: Wyndem
     * @DateTime: 2022-12-07 21:46
     */
    public static boolean checkMappingValue( List<MappingObj> source){
        boolean s1= false;
        for (MappingObj s : source){
            if (s1){
                break;
            }
            s1 =  MappingKit.isTargetValue(s);
        }
        return  s1;
    }


    /**
     * <p>
     *    查看是否有目标映射
     * </p>
     * @Author: Wyndem
     * @DateTime: 2022-12-07 21:39
     */
    public static boolean isTargetValue(MappingObj col){

        MappingType mappingType = MappingType.findByQueryType(col.getType());
        if (mappingType == null) {
            return false;
        }

        return (mappingType == MappingType.constant && StrUtil.isNotEmpty(col.getConstant())) ||

                (mappingType == MappingType.custom && StrUtil.isNotEmpty(col.getCustom()))   ||
                (mappingType == MappingType.sourceLookup &&  col.getSourceLookup() != null )   ||
                (mappingType == MappingType.column &&  StrUtil.isNotEmpty(col.getColumn()) );

    }

}
